package com.myhome.money.base.collection;

import java.util.*;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * @author 许路路
 */
public class QueueTest {

    private static Queue<String> stringQueue = new LinkedList<>();

    private static Queue<String> stringArrayDeque = new ArrayDeque<>();

    private static int queue_length = 200;

    private static String testStr = "{aaa:lklk,vvv:lll,;;;;;'''''ssss车牌号，位置，夹角，等sssssssswwwwwwwwwwww3444444444}";

    public static void main(String[] args) {
        queueLink();

        //priorityQueue();
        //getQueue();
    }

    public static void queueLink() {
        //init
        for (int i = 0; i < queue_length; i++) {
            Random rand = new Random();
            stringQueue.offer("name:" + rand.nextInt());
        }
        print(stringQueue);
        /*for (int i = queue_length; i > 0; i--) {
            while (stringQueue.size() >= queue_length) {
                stringQueue.poll();
            }
            if (stringQueue.size() <= queue_length) {
                stringQueue.offer("" + i + testStr);
            }
        }
        print(stringQueue);*/
    }

    public static void print(Queue<String> queue) {
        StringBuffer sb = new StringBuffer();
        Iterator<String> iterator = queue.iterator();
        while (iterator.hasNext()) {
            sb.append(iterator.next() + "_");
        }
        System.out.println(sb.toString().getBytes().length / 1024 + "|" + sb);
    }

    public static void priorityQueue() {
        Queue<Customer> integerPriorityQueue = new PriorityBlockingQueue<>(5);
        Random rand = new Random();
        for (int i = 0; i < 5; i++) {
            Customer customer = new Customer(new Integer(rand.nextInt(100)), "name" + i);
            integerPriorityQueue.offer(customer);
        }
        for (int i = 0; i < 5; i++) {
            Customer in = integerPriorityQueue.poll();
            System.out.println("Processing Integer:" + in);
        }
    }

    public static PriorityBlockingQueue<Customer> getQueue() {
        PriorityBlockingQueue<Customer> integerPriorityQueue = new PriorityBlockingQueue<>(5);
        Random rand = new Random();
        for (int i = 0; i < 5; i++) {
            Customer customer = new Customer(new Integer(rand.nextInt(100)), "name" + i);
            integerPriorityQueue.offer(customer);
        }
        integerPriorityQueue.poll();
        integerPriorityQueue.offer(new Customer(34, "name en"));
        System.out.println(integerPriorityQueue);
        return integerPriorityQueue;
    }

    public static class Customer implements Comparable<Customer> {

        private int id;
        private String name;

        public Customer(int i, String n) {
            this.id = i;
            this.name = n;
        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        @Override
        public int compareTo(Customer o) {
            return Integer.compare(this.id, o.getId());
        }

        @Override
        public String toString() {
            final StringBuffer sb = new StringBuffer("Customer{");
            sb.append("id=").append(id);
            sb.append(", name='").append(name).append('\'');
            sb.append('}');
            return sb.toString();
        }
    }

}
